//
//  GattManager.h
//  BlueDemo
//
//  Created by mac on 14-7-15.
//  Copyright (c) 2014年 izhihuicheng. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>
#import <CoreMotion/CoreMotion.h>

/** key快要过期的Block */
typedef void(^KeyWillOverdue)(NSString *key);

/** key已经的Block */
typedef void(^KeyBeenOverdue)(NSString *key);

/** 所有的key都过期的Block */
typedef void(^KeyAllOverdue)(NSArray *keys);


#define TranslateDataLenght   20

@protocol GattBLEDelegate <NSObject>

-(void)getBleSearchAnswer:(int)result resultString:(NSString *)str26;

@end


/**
 *  BLE 蓝牙处理类
 */
@interface GattManager : NSObject<CBCentralManagerDelegate,CBPeripheralDelegate,GattBLEDelegate>
{
    //连接等待次数
    int connectCounts;
    NSMutableArray *bleNameArray;
    int bleSearchCounts;
    int bleFlag;
    NSTimer *bleNameTimer;
    float bleNameTimerCount;
    CBUUID *service_uuid;
    CBUUID *char_uuid_w;
    CBUUID *char_uuid_n;
    
    NSTimer *connectTimer;
    NSTimer *openTimer;
    NSString *BLEName;
    
}


///中央端
@property(strong,nonatomic) CBCentralManager *centrelManager;
///外设端
@property(strong,nonatomic) CBPeripheral *targetPeripheral;
///发送给外设端的数据
@property(strong,nonatomic) NSData *postData;

@property(strong,nonatomic) NSMutableData *revicedData;
@property(strong, nonatomic) NSMutableData *receivedWeiData;

@property(strong,nonatomic) NSString *gattUserInfo;
@property(strong,nonatomic) NSString *gattUserFID;
@property(strong,nonatomic) NSArray *gattUserInfoArray;
@property(strong,nonatomic) NSArray *gattUserFIDArray;
@property(strong,nonatomic) NSArray *gattUserKeyArray;
@property(strong,nonatomic) NSArray *gattOpenKeyArray;
@property(strong,nonatomic) NSString *gattOpenKeyString;

@property(assign,nonatomic) id<GattBLEDelegate> delegate;

@property(strong,nonatomic)CMMotionManager*motionManager;

@property(nonatomic, strong) NSArray *allInfo;

///扫描次数
@property(assign,nonatomic) float countScan;
///默认扫描次数
@property(assign,nonatomic) float countSearch;
///发现服务失败次数
@property(assign,nonatomic) int serverAgain;
///发现特性失败次数
@property(assign,nonatomic) int charAgain;
///设置更新失败
@property(assign,nonatomic) int updateAgain;
///获得数据次数
@property(assign,nonatomic) int recDataCount;
///发现服务或特性失败后是否重连
@property(assign,nonatomic) BOOL isConnectAgain;
///添加数据的标志
@property(assign,nonatomic) BOOL tagAddData;
///添加V3数据的标志
@property(assign,nonatomic) BOOL tagV3AddData;
//微信包数据标志
@property(assign,nonatomic) BOOL tagWeiData;
//随机数
@property(assign,nonatomic) UInt32 randomData;

//FID位置
@property(assign,nonatomic) int fidCount;


-(void)decode:(NSArray *)keys
  willOverdue:(KeyWillOverdue)willOverdue
  beenOverdue:(KeyBeenOverdue)beenOverdue
   allOverdue:(KeyAllOverdue)allOverdue;

///将标志设置为初始值
-(void)setFlagDefault;

///创建中央端
-(void) setUp;

///中央端扫描外围
- (void)scan:(float)count;
///中央端停止扫描外围
-(void) stopScan;

///传输数据至外设端
-(void) write:(CBPeripheral *)peripheral data:(NSData *)data;
///读取数据
//-(void) read:(CBPeripheral *)peripheral;
///打印外设端的信息
- (void) printPeripheralInfo:(CBPeripheral*)peripheral;


-(void) notification:(int)serviceUUID characteristicUUID:(int)characteristicUUID p:(CBPeripheral *)p on:(BOOL)on;

-(UInt16) swap:(UInt16)s;

-(CBService *) findServiceFromUUIDEx:(CBUUID *)UUID p:(CBPeripheral *)p;
-(CBCharacteristic *) findCharacteristicFromUUIDEx:(CBUUID *)UUID service:(CBService*)service;
-(void) writeValue:(int)serviceUUID characteristicUUID:(int)characteristicUUID p:(CBPeripheral *)p data:(NSData *)data;
-(void) readValue: (int)serviceUUID characteristicUUID:(int)characteristicUUID p:(CBPeripheral *)p;
-(void) notify:(CBPeripheral *)peripheral on:(BOOL)isOn;
//-(id)initWithCode:(NSCoder*)aDecoder;
-(NSString *)getOpenKey;


//V2门禁二维码获取
- (NSString *)getQRCode:(NSArray *)codeKeyArray refreshInterval:(int)refreshInterval;


//V3门禁二维码获取
- (NSString *)createElevatorControllQRWithLLingId:(NSString *)llingId Keys:(NSArray *)keys  ValidTime:(int)validTime ValidNum:(int)validNum AuthLevel:(int)authLevel EncryptKey:(NSString *)encryptKey;


//梯控二维码获取
- (NSString *)createElevatorControllQRWithLLingId:(NSString *)llingId Keys:(NSArray *)keys StoryAuths:(NSArray *)storyAuths MinusFloor:(int)minusFloor ValidTime:(int)validTime ValidFloor:(int)validFloor AuthLevel:(int)authLevel EncryptKey:(NSString *)encryptKey;

@end
